﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Globalization
Imports System.Security.Permissions
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.ComponentModel.Design
    <PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust"), PermissionSet(SecurityAction.InheritanceDemand, Name:="FullTrust")> _
    Public Class TypeBrowserEditor
        Inherits UITypeEditor
        ' Methods
        Public Overrides Function EditValue(ByVal typeDescriptorContext As ITypeDescriptorContext, ByVal serviceProvider As IServiceProvider, ByVal value As Object) As Object
            If (typeDescriptorContext Is Nothing) Then
                Throw New ArgumentNullException("typeDescriptorContext")
            End If
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            Dim obj2 As Object = value
            Me.editorService = DirectCast(serviceProvider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
            If (Not Me.editorService Is Nothing) Then
                Dim filterProvider As ITypeFilterProvider = Nothing
                Dim attribute As TypeFilterProviderAttribute = Nothing
                If ((Not typeDescriptorContext.PropertyDescriptor Is Nothing) AndAlso (Not typeDescriptorContext.PropertyDescriptor.Attributes Is Nothing)) Then
                    attribute = [TryCast](typeDescriptorContext.PropertyDescriptor.Attributes.Item(GetType(TypeFilterProviderAttribute)),TypeFilterProviderAttribute)
                End If
                If (Not attribute Is Nothing) Then
                    If Not TypeOf serviceProvider.GetService(GetType(ITypeProvider)) Is ITypeProvider Then
                        Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
                    End If
                    Dim type As Type = Type.GetType(attribute.TypeFilterProviderTypeName)
                    If (Not type Is Nothing) Then
                        filterProvider = [TryCast](Activator.CreateInstance(type, New Object() { serviceProvider }),ITypeFilterProvider)
                    End If
                End If
                If (filterProvider Is Nothing) Then
                    filterProvider = [TryCast](If(TypeOf typeDescriptorContext.Instance Is Object(), DirectCast(DirectCast(typeDescriptorContext.Instance, Object())(0), ITypeFilterProvider), DirectCast(typeDescriptorContext.Instance, ITypeFilterProvider)),ITypeFilterProvider)
                End If
                If (filterProvider Is Nothing) Then
                    filterProvider = [TryCast](value,ITypeFilterProvider)
                End If
                If (filterProvider Is Nothing) Then
                    Dim service As IReferenceService = [TryCast](serviceProvider.GetService(GetType(IReferenceService)),IReferenceService)
                    If (Not service Is Nothing) Then
                        Dim component As IComponent = service.GetComponent(typeDescriptorContext.Instance)
                        If TypeOf component Is ITypeFilterProvider Then
                            filterProvider = [TryCast](component,ITypeFilterProvider)
                        End If
                    End If
                End If
                If (filterProvider Is Nothing) Then
                    filterProvider = [TryCast](typeDescriptorContext.PropertyDescriptor,ITypeFilterProvider)
                End If
                Dim selectedTypeName As String = [TryCast](value,String)
                If (((Not value Is Nothing) AndAlso (Not typeDescriptorContext.PropertyDescriptor.PropertyType Is GetType(String))) AndAlso ((Not typeDescriptorContext.PropertyDescriptor.Converter Is Nothing) AndAlso typeDescriptorContext.PropertyDescriptor.Converter.CanConvertTo(GetType(String)))) Then
                    selectedTypeName = [TryCast](typeDescriptorContext.PropertyDescriptor.Converter.ConvertTo(typeDescriptorContext, CultureInfo.CurrentCulture, value, GetType(String)),String)
                End If
                Dim dialog As New TypeBrowserDialog(serviceProvider, filterProvider, selectedTypeName)
                If (DialogResult.OK <> Me.editorService.ShowDialog(dialog)) Then
                    Return obj2
                End If
                If (typeDescriptorContext.PropertyDescriptor.PropertyType Is GetType(Type)) Then
                    Return dialog.SelectedType
                End If
                If (typeDescriptorContext.PropertyDescriptor.PropertyType Is GetType(String)) Then
                    Return dialog.SelectedType.FullName
                End If
                If ((Not typeDescriptorContext.PropertyDescriptor.Converter Is Nothing) AndAlso typeDescriptorContext.PropertyDescriptor.Converter.CanConvertFrom(typeDescriptorContext, GetType(String))) Then
                    obj2 = typeDescriptorContext.PropertyDescriptor.Converter.ConvertFrom(typeDescriptorContext, CultureInfo.CurrentCulture, dialog.SelectedType.FullName)
                End If
            End If
            Return obj2
        End Function

        Public Overrides Function GetEditStyle(ByVal typeDescriptorContext As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.Modal
        End Function


        ' Fields
        Private editorService As IWindowsFormsEditorService
    End Class
End Namespace

